package com.xdf.notifier;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * 不使用报警邮件功能，则使用本功能实现自定义事件通知功能
 * @author chanchaw
 * @create 2025-08-19 9:28
 */
@Slf4j
@Component
public class MyCustomNotifier extends AbstractEventNotifier {
    protected MyCustomNotifier(InstanceRepository repository){
        super(repository);
    }
    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() ->{
            if(event instanceof InstanceStatusChangedEvent){// 状态变更才执行，启动后成功运行不会触发该事件，因为之前没有状态，所以不算作状态变更
                log.info("Instance status changed:[{}],[{}],[{}]",
                        instance.getRegistration().getName(),
                        event.getInstance(),
                        ((InstanceStatusChangedEvent)event).getStatusInfo().getStatus());
            }else{
                log.info("Instance Info:[{}],[{}],[{}]",
                        instance.getRegistration().getName(),event.getInstance(),event.getType());
            }
        });
    }
}
